package org.jeecg;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.util.oConvertUtils;
import org.opencv.core.Core;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.Environment;

import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
* 单体启动类
* 报错提醒: 未集成mongo报错，可以打开启动类上面的注释 exclude={MongoAutoConfiguration.class}
*/
@Slf4j
@ComponentScan(value = {"org.jeecg","cn.cuiot","org.wgai"})
@SpringBootApplication
//@EnableAutoConfiguration(exclude={MongoAutoConfiguration.class})
public class JeecgSystemApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(JeecgSystemApplication.class);
    }



    public static void main(String[] args) throws UnknownHostException {
       // System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

       // System.load("C:\\JAVAAI\\opencv\\build\\java\\x64\\opencv_java481.dll");
        ConfigurableApplicationContext application = SpringApplication.run(JeecgSystemApplication.class, args);
        Environment env = application.getEnvironment();
        String ip = InetAddress.getLocalHost().getHostAddress();
        String port = env.getProperty("server.port");
        String path = oConvertUtils.getString(env.getProperty("server.servlet.context-path"));
        String opencvpath = env.getProperty("opencv");
        String audiopath = env.getProperty("audio.dll");


        log.info("\n----------------------------------------------------------\n\t" +
                "Application Jeecg-Boot is running! Access URLs:\n\t" +
                "Local: \t\thttp://localhost:" + port + path + "/\n\t" +
                "External: \thttp://" + ip + ":" + port + path + "/\n\t" +
                "Swagger文档: \thttp://" + ip + ":" + port + path + "/doc.html\n\t" +
                "opencvpath:"+opencvpath+"\n\t  "+
                "audio:"+audiopath+"\n\t  "+
                "----------------------------------------------------------");
        if(StringUtils.isNotEmpty(opencvpath)){
            String [] opencvList=opencvpath.split(";");
            for (int i = 0; i <opencvList.length ; i++) {
                File opencv=new File(opencvList[i]);
                if(opencv.exists()){
                    System.load(opencvList[i]);
                   // log.info(Core.getBuildInformation());
                    log.info("opencv加载成功");
                    break;
                }else{
                    log.error("opencv文件不存在！请检查地址是否正确 或 是否编译opencv");
                }
            }
        }

        if(StringUtils.isNotEmpty(audiopath)){
            String [] audiopathList=audiopath.split(";");
            for (int i = 0; i <audiopathList.length ; i++) {
                File opencv=new File(audiopathList[i]);
                if(opencv.exists()){
                    System.load(audiopathList[i]);
                    log.info("audio加载成功");
                    break;
                }else{
                    log.error("audio文件不存在！请检查地址是否正确 或 是否编译audio");
                }
            }
        }

    }

}